<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <form id="my-form" class="am-form tpl-form-line-form" method="post">
                    <input type="hidden" name="s" value="">
                    <input type="hidden" name="step" value="2">
                    <div class="widget-body">
                        <fieldset>
                            <div class="widget-head am-cf">
                                <div class="widget-title am-fl">编辑奖品</div>
                            </div>
                            <div class="goods-spec-single">
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">名称 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <input type="text" class="tpl-form-input" name="goods[name]" value="<?= $good['name']?>" required>
                                        <div class="help-block">
                                            <small>例如：谢谢参与</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">奖品logo </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <div class="am-form-file">
                                            <div class="am-form-file">
                                                <button type="button"
                                                        class="select-logo upload-file am-btn am-btn-secondary am-radius">
                                                    <i class="am-icon-cloud-upload"></i> 选择图片
                                                </button>
                                                <div class="uploader-list am-cf">
                                                    <div class="file-item">
                                                        <a href="<?= $good['logo']['file_path'] ?>" title="点击查看大图" target="_blank">
                                                            <img src="<?= $good['logo']['file_path'] ?>">
                                                        </a>
                                                        <input type="hidden" name="goods[logo_id]" value="<?= $good['logo_id'] ?>">
                                                        <i class="iconfont icon-shanchu file-item-delete"></i>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="help-block am-margin-top-sm">
                                                <small>尺寸750x750像素以上，大小2M以下 (可拖拽图片调整显示顺序 )</small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">中奖值 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <div class="am-input-group">
                                            <span class="am-input-group-label am-input-group-label__left">万分之</span>
                                            <input type="number" class="am-form-field" name="goods[value]" min="0" value="<?= $good['value'] ?>" required>
                                        </div>
                                        <div class="help-block am-margin-top-sm">
                                            <small>说明：该商品的中奖率为 设置的中奖值 * 库存，当库存为0时，即中奖率为0</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">奖品类型 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <label class="am-radio-inline">
                                            <input type="radio" name="goods[type]" value="1" data-am-ucheck
                                            <?php echo $good['type']['text'] == 1 ? 'checked' : ''; ?>
                                            >
                                            谢谢参与
                                        </label>
                                        <div class="am-margin-top-xs">
                                            <label class="am-radio-inline">
                                                <input type="radio" name="goods[type]" value="2" data-am-ucheck
                                                    <?php echo $good['type']['text'] == 2 ? 'checked' : ''; ?>
                                                >
                                                积分
                                            </label>
                                            <div class="am-input-group am-padding-top">
                                                <span class="am-input-group-label am-input-group-label__left">赠送</span>
                                                <input type="number" name="goods[integral]" class="am-form-field" min="0" value="<?= $good['prize']?>">
                                                <span class="am-input-group-label am-input-group-label__right">积分</span>
                                            </div>
                                        </div>
                                        <div class="am-margin-top-xs">
                                            <label class="am-radio-inline">
                                                <input type="radio" name="goods[type]" value="3" data-am-ucheck
                                                <?php echo $good['type']['text'] == 3 ? 'checked' : ''; ?>
                                                >
                                                优惠券
                                            </label>
                                            <div class="am-form-file am-margin-top-xs">
                                                <button type="button"
                                                        class="j-selectCoupons upload-file am-btn am-btn-secondary am-radius">
                                                    <i class="am-icon-cloud-upload"></i> 选择优惠券
                                                </button>
                                                <div class="widget-coupons-list uploader-list am-cf am-margin-top-xs">
                                                    <?php if ($good['type']['text'] == 3): ?>
                                                    <span class="am-btn-sm am-btn-success" style="padding: 5px;"><?= $good['prize']['name'] ?></span>
                                                    <input type="hidden" name="goods[coupon_id]" value="<?= $good['prize']['coupon_id'] ?>">
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                            <div class="help-block">
                                                <small>注：优惠券时必须选择，且只选一个</small>
                                            </div>
                                        </div>
                                        <div class="am-margin-top-xs">
                                            <label class="am-radio-inline">
                                                <input type="radio" name="goods[type]" value="4" data-am-ucheck
                                                <?php echo $good['type']['text'] == 4 ? 'checked' : ''; ?>
                                                >
                                                赠品
                                            </label>
                                            <div class="am-form-file am-margin-top-xs">
                                                <button type="button"
                                                        class="j-selectGoods upload-file am-btn am-btn-secondary am-radius">
                                                    <i class="am-icon-cloud-upload"></i> 选择赠品
                                                </button>
                                                <div class="widget-goods-list uploader-list am-cf">
                                                    <?php if ($good['type']['text'] == 4): ?>
                                                    <div class="file-item">
                                                        <a href="<?= $good['prize']['image'][0]['file_path'] ?>" title="赠品" target="_blank">
                                                            <img src="<?= $good['prize']['image'][0]['file_path'] ?>">
                                                        </a>
                                                        <input type="hidden" name="goods[goods_id]" value="<?= $good['prize']['goods_id'] ?>">
                                                    </div>
                                                    <?php endif; ?>
                                                </div>
                                            </div>
                                            <div class="help-block">
                                                <small>注：赠品时必须选择，且只选一个</small>
                                            </div>
                                        </div>
                                    </div>

                                </div>

                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">库存 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <input type="number" class="tpl-form-input" name="goods[num]" min="0" value="<?= $good['num'] ?>" required>
                                        <div class="help-block">
                                            <small>例如：</small>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">排序 </label>
                                    <div class="am-u-sm-9 am-u-md-6 am-u-lg-5 am-u-end">
                                        <input type="number" class="tpl-form-input" name="goods[sort]" min="0" value="<?= $good['sort'] ?>" required>
                                    </div>
                                    <div class="help-block">
                                        <small>数字越小越靠前</small>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <div class="am-u-sm-10 am-u-sm-push-2 am-margin-top-lg">
                                        <button type="submit" class="j-submit am-btn am-btn-sm am-btn-secondary">提交
                                        </button>
                                    </div>
                                </div>
                        </fieldset>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 图片文件列表模板 -->
{{include file="layouts/_template/tpl_file_item" /}}

<!-- 文件库弹窗 -->
{{include file="layouts/_template/file_library" /}}

<!-- 优惠券列表 -->
<script id="tpl-coupons-list-item" type="text/template">
    {{ each $data }}
        <span class="am-btn-sm am-btn-success" style="padding: 5px;">{{ $value.name }}</span>
        <input type="hidden" name="goods[coupon_id]" value="{{ $value.coupon_id }}">
    {{ /each }}
</script>

<!-- 商品列表 -->
<script id="tpl-goods-list-item" type="text/template">
    {{ each $data }}
    <div class="file-item">
        <a href="{{ $value.image }}" title="{{ $value.goods_name }}" target="_blank">
            <img src="{{ $value.image }}">
        </a>
        <input type="hidden" name="goods[goods_id]" value="{{ $value.goods_id }}">
    </div>
    {{ /each }}
</script>

<script src="assets/common/js/ddsort.js"></script>
<script src="assets/store/js/select.data.js?v=<?= $version ?>"></script>
<script>
    $(function () {

        // 选择图片
        $('.select-logo').selectImages({
            name: 'goods[logo_id]',
            multiple: false
        });

        // 图片列表拖动
        $('.uploader-list').DDSort({
            target: '.file-item',
            delay: 100, // 延时处理，默认为 50 ms，防止手抖点击 A 链接无效
            floatStyle: {
                'border': '1px solid #ccc',
                'background-color': '#fff'
            }
        });

        // 选择优惠券
        $('.j-selectCoupons').selectData({
            title: '选择优惠券',
            uri: 'coupon/lists',
            dataIndex: 'coupon_id',
            done: function (data) {
                data = [data[0]]
                var $html = $(template('tpl-coupons-list-item', data));
                $('.widget-coupons-list').html($html);
            }
        });

        // 选择商品
        $('.j-selectGoods').selectData({
            title: '选择商品',
            uri: 'goods/lists',
            dataIndex: 'goods_id',
            done: function (data) {
                data = [data[0]];
                var $html = $(template('tpl-goods-list-item', data));
                $('.widget-goods-list').html($html);
            }
        });

        //切换移除数据
        $('input:radio[name="goods[type]"]').change(function (data) {
            if (data.currentTarget.value != 3) {
                var $html = $(template('tpl-coupons-list-item', []));
                $('.widget-coupons-list').html($html);
            }
            if (data.currentTarget.value != 4) {
                var $html = $(template('tpl-goods-list-item', []));
                $('.widget-goods-list').html($html);
            }
        })


        /**
         * 表单验证提交
         * @type {*}
         */
        $('#my-form').superForm({
            // 自定义验证
            validation: function () {
                let logo = $('.uploader-list').find('input[type="hidden"][name="goods[logo_id]"]').val()
                if (!logo) {
                    layer.msg("请选择奖品logo")
                    return false
                }
                let type = $('input:radio[name="goods[type]"]:checked').val()
                switch (parseInt(type)) {
                    //积分
                    case 2:
                        let integral = $('.am-input-group').find('input[type="number"][name="goods[integral]"]').val()
                        if (integral <= 0) {
                            layer.msg("积分不能为0")
                            return false
                        }
                        break;
                    //优惠券
                    case 3:
                        let coupon_id = $('input:hidden[name="goods[coupon_id]"]').val()
                        if (!coupon_id) {
                            layer.msg("请选择优惠券")
                            return false
                        }
                        break;
                    case 4:
                        let goods_id = $('input:hidden[name="goods[goods_id]"]').val()
                        if (!goods_id) {
                            layer.msg("请选择赠品")
                            return false
                        }
                        break;
                }
                return true;
            }

        });
    });
</script>
